class CharData:
	var id
	var level:int
	var hp:int
	var battleHp:int
	
	var attack:int
	var skill:int
	var speed:int
	var luck:int
	var defense:int
	var magicDefense:int
	
	var move:int
	var exp:int
	
	var charConfig
	var careerConfig
	var weaponTypeConfig
	
	# battle
	var hit
	var terrainAvoid 
	
	
	func initByCharConfig(_charConfigId) -> void:
		var config = ConfigClasses.CharacterConfig.idConfigDict[_charConfigId]
		charConfig = config
		
		careerConfig = ConfigClasses.CareerConfig.idConfigDict[charConfig.career]
		weaponTypeConfig = ConfigClasses.WeaponTypeConfig.idConfigDict[charConfig.weaponType]
		
		id = charConfig.id
		level = int(charConfig.level) 
		hp = int(charConfig.hp)
		battleHp = hp
		attack = int(charConfig.attack)
		skill = int(charConfig.skill)
		speed = int(charConfig.speed)
		luck = int(charConfig.luck)
		defense = int(charConfig.defense)
		magicDefense = int(charConfig.magicDefense)
		move = int(charConfig.move) 
		exp = 0
		hit = 0
		terrainAvoid = 0
		
		
	func attackData(other:CharData):
		var result = {}
		
		var attrModConfig = ConfigClasses.AttributeModifierConfig.configArr[0]
		var skillHit = int(attrModConfig.skillHit)
		var speedAvoid = int(attrModConfig.speedAvoid)
		var luckAvoid = int(attrModConfig.luckAvoid)
		var skillCritical = int(attrModConfig.skillCritical)
		var luckCritical = int(attrModConfig.luckCritical)
		var criticalDamage = int(attrModConfig.criticalDamage)
		var doubleAttackSpeedDiff = int(attrModConfig.doubleAttackSpeedDiff)
		
		result["hit"] = skill * skillHit  + int(weaponTypeConfig.hit) - (other.speed * speedAvoid + other.luck * luckAvoid + other.terrainAvoid)
		result["critical"] = int(weaponTypeConfig.critical) + skill * skillCritical + (luck - other.luck) * luckCritical
		result["damage"] = attack - other.defense
		var speedDiff = speed - other.speed
		if speedDiff >= 4: result["doubleAttack"] = true
		else: result["doubleAttack"] = false
		
		return result
		
		
	func simulateAttackResult(other):
		var attrModConfig = ConfigClasses.AttributeModifierConfig.configArr[0]
		var criticalDamageMod = int(attrModConfig.criticalDamage)
		
		var result = {}
		
		var attackData = attackData(other)
		var oppositeData = other.attackData(self)  
		
		# first attack
		var isHit = false
		var r = randf() * 100
		if r <= attackData["hit"]:
			isHit = true
		
		var isCritical = false
		r = randf() * 100
		if r <= attackData["critical"]:
			isCritical = true
			
		result["firstCritical"] = false
		if not isHit:
			result["firstActualDamage"] = 0
		elif isCritical:
			result["firstCritical"] = true
			result["firstActualDamage"] = attackData["damage"] * criticalDamageMod
		else:
			result["firstActualDamage"] = attackData["damage"]
		
		if other.battleHp <= result["firstActualDamage"]:
			result["firstAttackTargetDie"] = true
			result["firstAttackTargetHp"] = 0
			return result
		else:
			result["firstAttackTargetDie"] = false
			result["firstAttackTargetHp"] = other.battleHp - result["firstActualDamage"]
		
		# second fanji
		isHit = false
		r = randf() * 100
		if r <= oppositeData["hit"]:
			isHit = true
			
		isCritical = false
		r = randf() * 100
		if r <= oppositeData["critical"]:
			isCritical = true
			
		result["fanjiCritical"] = false
		if not isHit:
			result["fanjiActualDamage"] = 0
		elif isCritical:
			result["fanjiCritical"] = true
			result["fanjiActualDamage"] = oppositeData["damage"] * criticalDamageMod
		else:
			result["fanjiActualDamage"] = oppositeData["damage"]
		
		if battleHp <= result["fanjiActualDamage"]:
			result["fanjiAttackTargetDie"] = true
			result["fanjiAttackTargetHp"] = 0
			return result
		else:
			result["fanjiAttackTargetDie"] = false
			result["fanjiAttackTargetHp"] = battleHp - result["fanjiActualDamage"]
		
		# zhuiji
		if not attackData["doubleAttack"]: return result
		
		isHit = false
		r = randf() * 100
		if r <= attackData["hit"]:
			isHit = true
			
		isCritical = false
		r = randf() * 100
		if r <= attackData["critical"]:
			isCritical = true
			
		result["zhuijiCritical"] = false
		if not isHit:
			result["zhuijiActualDamage"] = 0
		elif isCritical:
			result["zhuijiCritical"] = true
			result["zhuijiActualDamage"] = attackData["damage"] * criticalDamageMod
		else:
			result["zhuijiActualDamage"] = attackData["damage"]
		
		if other.battleHp <= result["zhuijiActualDamage"]:
			result["zhuijiAttackTargetDie"] = true
			result["zhuijiAttackTargetHp"] = 0
			return result
		else:
			result["zhuijiAttackTargetDie"] = false
			result["zhuijiAttackTargetHp"] = other.battleHp - result["zhuijiActualDamage"]
		
		return result
